import { logger } from '../core/logger';
import { clientBase, HOST } from './clientBase';
import { ajaxResult } from '../models/ajaxResult';
import { deviceInfo } from '../models/deviceInfo';
import { loginResult } from '../models/loginResult';
import axios, { AxiosError, AxiosResponse } from '@ohos/axios';

class deviceClient extends clientBase {
  login(areaCode: string, deviceMac: string): Promise<ajaxResult<loginResult>> {
    return new Promise((resolve) => {
      axios.post<string, AxiosResponse<string>, any>(`${HOST}api/devices/login`, {
        areaCode: areaCode,
        deviceMac: deviceMac
      }).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: loginResult = response.data.result;
          resolve(this.buildSuccessResult<loginResult>(ret));
        } else
          resolve(this.buildFailedResult<loginResult>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<loginResult>(err));
      });
    });
  }

  create(data: deviceInfo): Promise<ajaxResult<string>> {
    return new Promise((resolve) => {
      axios.post<string, AxiosResponse<string>, any>(`${HOST}api/devices/create`, data, this.withToken).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: string = response.data.result;
          resolve(this.buildSuccessResult<string>(ret));
        } else
          resolve(this.buildFailedResult<string>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<string>(err));
      });
    });
  }

  update(data: deviceInfo): Promise<ajaxResult<string>> {
    return new Promise((resolve) => {
      axios.post<string, AxiosResponse<string>, any>(`${HOST}api/devices/update`, data, this.withToken).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: string = response.data.result;
          resolve(this.buildSuccessResult<string>(ret));
        } else
          resolve(this.buildFailedResult<string>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<string>(err));
      });
    });
  }

  delete(deviceId: string): Promise<ajaxResult<string>> {
    return new Promise((resolve) => {
      axios.delete<string, AxiosResponse<string>, any>(`${HOST}api/devices/del?deviceId=${deviceId}`, this.withToken).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: string = response.data.result;
          resolve(this.buildSuccessResult<string>(ret));
        } else
          resolve(this.buildFailedResult<string>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<string>(err));
      });
    });
  }

  getControl(areaCode: string): Promise<ajaxResult<Array<deviceInfo>>> {
    return new Promise((resolve) => {
      axios.get<string, AxiosResponse<string>, null>(`${HOST}api/devices/getControl?areaCode=${areaCode}`, this.withToken).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: Array<deviceInfo> = response.data.result;
          resolve(this.buildSuccessResult<Array<deviceInfo>>(ret));
        } else
          resolve(this.buildFailedResult<Array<deviceInfo>>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<Array<deviceInfo>>(err));
      });
    });
  }

  getDevices(areaCode: string): Promise<ajaxResult<Array<deviceInfo>>> {
    return new Promise((resolve) => {
      axios.get<string, AxiosResponse<string>, null>(`${HOST}api/devices/getDevices?areaCode=${areaCode}`, this.withToken).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: Array<deviceInfo> = response.data.result;
          resolve(this.buildSuccessResult<Array<deviceInfo>>(ret));
        } else
          resolve(this.buildFailedResult<Array<deviceInfo>>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<Array<deviceInfo>>(err));
      });
    });
  }

  getDeviceByMac(mac: string): Promise<ajaxResult<deviceInfo>> {
    return new Promise((resolve) => {
      axios.get<string, AxiosResponse<string>, null>(`${HOST}api/devices/getDeviceByMac?mac=${mac}`, this.withToken).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: deviceInfo = response.data.result;
          resolve(this.buildSuccessResult<deviceInfo>(ret));
        } else
          resolve(this.buildFailedResult<deviceInfo>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<deviceInfo>(err));
      });
    });
  }

  getAllDevices(areaCode: string): Promise<ajaxResult<Array<deviceInfo>>> {
    return new Promise((resolve) => {
      axios.get<string, AxiosResponse<string>, null>(`${HOST}api/devices/getAllDevices?areaCode=${areaCode}`, this.withToken).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: Array<deviceInfo> = response.data.result;
          resolve(this.buildSuccessResult<Array<deviceInfo>>(ret));
        } else
          resolve(this.buildFailedResult<Array<deviceInfo>>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<Array<deviceInfo>>(err));
      });
    });
  }

  getStatus(deviceId: string): Promise<ajaxResult<number>> {
    return new Promise((resolve) => {
      axios.get<string, AxiosResponse<string>, null>(`${HOST}api/devices/getStatus?deviceId=${deviceId}`, this.withToken).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: number = response.data.result;
          resolve(this.buildSuccessResult<number>(ret));
        } else
          resolve(this.buildFailedResult<number>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<number>(err));
      });
    });
  }

  getPaging(pageIndex: number, keywords: string): Promise<ajaxResult<Array<deviceInfo>>> {
    return new Promise((resolve) => {
      axios.get<string, AxiosResponse<string>, null>(`${HOST}api/devices/getPaging?pageIndex=${pageIndex}&keywords=${keywords}`, this.withToken).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: Array<deviceInfo> = response.data.result?.data ?? [];
          resolve(this.buildSuccessResult<Array<deviceInfo>>(ret));
        } else
          resolve(this.buildFailedResult<Array<deviceInfo>>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<Array<deviceInfo>>(err));
      });
    });
  }
}
export default new deviceClient();